﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Silverlight.Navigation.Mapping;

namespace Silverlight.Navigation.Controls
{
    public partial class ApplicationMapPath : UserControl
    {
        #region Events

        /// <summary>
        /// The navigate to home event.
        /// </summary>
        public event HomeNavigationEventHandler NavigateToHome;

        /// <summary>
        /// The navigate to main menu event.
        /// </summary>
        public event MainMenuNavigationEventHandler NavigateToMainMenu;

        /// <summary>
        /// The navigate to submenu event.
        /// </summary>
        public event SubMenuNavigationEventHandler NavigateToSubMenu;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the ApplicationMapPath class.
        /// </summary>
        public ApplicationMapPath()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the ApplicationMapPath class.
        /// </summary>
        /// <param name="path"></param>
        public ApplicationMapPath(MenuItemCollection path)
            : this()
        {
            if (path == null)
                throw new ArgumentNullException("path");

            this.Path = path;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The path to the current location in the application.
        /// </summary>
        public MenuItemCollection Path 
        {
            get
            {
                if (this.MenuItemsControl.ItemsSource != null)
                    return (MenuItemCollection)this.MenuItemsControl.ItemsSource;
                else
                    return null;
            }
            set
            {
                this.MenuItemsControl.ItemsSource = value;
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Handles the click event of the hyperlink button.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            if (sender is HyperlinkButton)
            {
                HyperlinkButton source = (HyperlinkButton)sender;

                if (source.DataContext != null)
                {
                    if (source.DataContext is HomeMenuItem)
                    {
                        if (NavigateToHome != null)
                            NavigateToHome(this, new HomeNavigationEventArgs((HomeMenuItem)source.DataContext));
                    }
                    else if (source.DataContext is SubMenuItem)
                    {
                        if (NavigateToSubMenu != null)
                            NavigateToSubMenu(this, new SubMenuNavigationEventArgs((SubMenuItem)source.DataContext));
                    }
                    else if (source.DataContext is MainMenuItem)
                    {
                        if (NavigateToMainMenu != null)
                            NavigateToMainMenu(this, new MainMenuNavigationEventArgs((MainMenuItem)source.DataContext));
                    }
                }
            }
        }

        #endregion
    }
}